package com.example.lenovo.demo.presenter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.example.lenovo.demo.MyApplication;
import com.example.lenovo.demo.R;
import com.example.lenovo.demo.StrategyActivity;
import com.example.lenovo.demo.model.databean.CityBean;
import com.example.lenovo.demo.model.databean.ModelDaysBean;
import com.example.lenovo.demo.view.MapFragment;
import com.example.lenovo.demo.view.ifgview.IMapActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Lenovo on 2017/1/13.
 */

public class MapActivityPresenter implements IMapActivity,RadioGroup.OnCheckedChangeListener {
    private Context context;
    private MapView mapView;
    private RadioGroup radioGroup;
    private BaiduMap baiduMap;
    private GeoCoder geoCoder;
    private LatLng latLng;
    private Bitmap bitmap;
    private BitmapDescriptor bitmaps ;
    private List<OverlayOptions> optionses;
    private DrawerLayout drawerLayout;
    private FragmentManager manager;
    private List<ModelDaysBean.DataBean.DaysBean.PointsBean> points;
    private int num;
    private IMapActivity iMapActivity;
    private ModelDaysBean modelDaysBean;

    @Override
    public void sendView(Context context, MapView mapView, int num, FragmentManager manager,DrawerLayout drawerLayout,ModelDaysBean modelDaysBean,RadioGroup radioGroup){
        this.context=context;
        this.mapView=mapView;
        this.num=num;
        this.manager=manager;
        this.drawerLayout=drawerLayout;
        this.modelDaysBean=modelDaysBean;
        this.radioGroup=radioGroup;
    }

    public void initData(){
        baiduMap=mapView.getMap();
        optionses=new ArrayList<>();
        initMainLocation(mapView,modelDaysBean.getData().getDestination().getLat(),modelDaysBean.getData().getDestination().getLng());
        bitmap= BitmapFactory.decodeResource(context.getResources(), R.drawable.location);
        initBitMap();
        points=modelDaysBean.getData().getDays().get(num).getPoints();
        for (int i= 0; i< points.size(); i++) {
            for (ModelDaysBean.DataBean.DaysBean.PointsBean point : points) {
                latLng=new LatLng(point.getPoi().getBlat(),point.getPoi().getBlng());
                OverlayOptions option = new MarkerOptions()
                        .position(latLng)
                        .icon(bitmaps);
                optionses.add(option);
            }
        }
        baiduMap.addOverlays(optionses);

        FragmentTransaction ft=manager.beginTransaction();
        Bundle bundle=new Bundle();
        bundle.putParcelable("msg",modelDaysBean);
        MapFragment mapFragment=new MapFragment();
        mapFragment.setArguments(bundle);
        ft.add(R.id.fragment_citymap_drawlayout,mapFragment);
        ft.commit();
        //打开抽屉，指定的方向
        drawerLayout.openDrawer(Gravity.RIGHT);
        radioGroup.setOnCheckedChangeListener(this);
    }

    private void initMainLocation(MapView mapView, double lat, double lng) {
        LatLng cenpt = new LatLng(lat,lng);
        //定义地图状态
        MapStatus mMapStatus = new MapStatus.Builder()
                .target(cenpt)
                .zoom(12)
                .build();
        MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
        mapView.getMap().setMapStatus(mMapStatusUpdate);
    }
    private void initBitMap() {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        // 设置想要的大小
        int newWidth = 40;
        int newHeight = 40;
        // 计算缩放比例
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // 取得想要缩放的matrix参数
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        // 得到新的图片
        bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix,
                true);

        bitmaps = BitmapDescriptorFactory.fromBitmap(bitmap);
    }


    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i){
           case R.id.activity_map_rb1:
               Toast.makeText(context,"未找到地图资源",Toast.LENGTH_SHORT).show();
               break;
            case R.id.activity_map_rb2:
                Intent intent=new Intent(context, StrategyActivity.class);
                intent.putExtra("msg",MyApplication.id+"");
                context.startActivity(intent);
                break;
        }
    }
}
